//
//  ZKHomeTableViewController.swift
//  Weibo
//
//  Created by 郑克哥哥 on 2017/7/22.
//  Copyright © 2017年 郑克哥哥. All rights reserved.
//

import UIKit

import YYModel

class ZKHomeTableViewController: ZKVisitorTableViewController {
    
    //tableView数据源
//    lazy var statusList: [StatusModel] = [StatusModel]()
    //引用ViewModel
    fileprivate lazy var homeViewModel: ZKHomeViewModel = ZKHomeViewModel()
    
    //MARK: -------懒加载控件-------
    //上拉加载控件
    fileprivate lazy var pullUpView: UIActivityIndicatorView = {
        
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        view.color = UIColor.orange
        
        return view
    }()
    //下拉加载控件
//    fileprivate lazy var pullDownView:UIRefreshControl = {
//    
//        let view = UIRefreshControl()
//        //添加事件
//        view.addTarget(self, action: #selector(pullDownRefreshing), for: .valueChanged)
//    
//        return view
//    }()
    //自定义下拉加载控件
    fileprivate lazy var pullDownView:ZKRefreshControl = {
        
        let view = ZKRefreshControl()
        //添加事件
        view.addTarget(self, action: #selector(pullDownRefreshing), for: .valueChanged)
        
        return view
    }()
    
    //添加tipLabel
    fileprivate lazy var tipLabel:UILabel = {
    
        let label = UILabel(font: 12, textColor: UIColor.white)
        label.backgroundColor = UIColor.orange
        label.text = "没有最新数据"
        label.textAlignment = .center
        //默认隐藏tipLabel
        label.isHidden = true
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //判断是否进行登录状态
        if !islogin {
            
            //修改访客视图信息
            visitorView?.updateVisitorInfo(text: nil, imageName: nil)
        }else{
            
            //登录
            loadData()

            setupUI()
        }
    }
    
    //添加子控件
    private func setupUI() {
     
        if let nav = self.navigationController {
            //设置控件在导航栏下面
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
            tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35, width: nav.navigationBar.width, height: 35)
        }
        
        setupTableView()
        
    }
    
    //设置tableView相关操作
    private func setupTableView(){
        
        //注册cell
        tableView.register(ZKHomeTableViewCell.self, forCellReuseIdentifier: HomeCell)
        
        //设置cell的高度,自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        
        //设置预估高度
        tableView.estimatedRowHeight = 200
        
        //去掉分割线
        tableView.separatorStyle = .none
        
        //设置上拉加载视图
        tableView.tableFooterView = pullUpView
        
        //添加系统下拉刷新
        //tableView.addSubview(pullDownView)
        //添加自定义下拉刷新
        tableView.addSubview(pullDownView)
    }
    
    //加载微博数据
    func loadData(){
        
        homeViewModel.loadData(isUp: pullUpView.isAnimating) { (isSuccess,message) in
            //不是上拉加载执行tip动画
            if self.pullUpView.isAnimating == false{
                //执行tip动画
                self.startTipAnimation(message: message)
            }
            //结束刷新
            self.endRefreshing()
            if isSuccess {
                
                self.tableView.reloadData()
            }
        }
    }
    
    //MARK: -------tipLabel动画-------
    private func startTipAnimation(message:String){
        //不让动画重复显示
        if tipLabel.isHidden == false {
            return
        }
        tipLabel.isHidden = false
        
        tipLabel.text = message
        
        UIView.animate(withDuration: 1, animations: { 
            self.tipLabel.transform = CGAffineTransform(translationX: 0, y: self.tipLabel.height)
        }) { (_) in
            UIView.animate(withDuration: 1, animations: { 
                self.tipLabel.transform = CGAffineTransform.identity
            }, completion: { (_) in
                self.tipLabel.isHidden = true
            })
        }
        
        
        
    }
    
    //下拉刷新
    @objc private func pullDownRefreshing(){
        
        print("下拉刷新")
        loadData()
        
    }
    
    //结束刷新
    private func endRefreshing() {
        
        //结束上拉停止动画
        pullUpView.stopAnimating()
        //结束下拉刷新
        pullDownView.endRefreshing()
    }

}


extension ZKHomeTableViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: HomeCell, for: indexPath) as! ZKHomeTableViewCell
        //获取模型
        let statusViewModel = homeViewModel.statusList[indexPath.row]
        
        //绑定数据
        cell.statusViewModel = statusViewModel
        //取消cell选中颜色
        cell.selectionStyle = .none
        return cell
    }
    
    //将要显示的cell
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        if indexPath.row == homeViewModel.statusList.count - 1 && pullUpView.isAnimating == false{
            
            print("最后一个cell")
            //开启pullUpView的动画
            pullUpView.startAnimating()
            
            //加载微博数据
            loadData()
            
        }
        
        
    }
    
}
